package com.example.wineknower.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.SslError;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.fastjson.JSONObject;
import com.example.wineknower.R;
import com.example.wineknower.fragment.ZhiDaoDetailsFragment;
import com.example.wineknower.method.OnClickShare;
import com.example.wineknower.model.article;
import com.example.wineknower.utils.OkHttpUtil;
import com.example.wineknower.utils.UserUtil;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

public class ZhiDaoDetailsActivity extends AppCompatActivity {

    private Tencent mTencent;
    private String APP_ID = "1110111320";
    MainActivity mainActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhidaodetails_activity);
        Intent i=getIntent();
        String url = i.getStringExtra("url");
        System.out.println("webview_url = " + url);

        WebView webview = this.findViewById(R.id.zhidao_webview);

            webview.setWebViewClient(new WebViewClient());//非常非常重要的单webviewclient 让他不打开浏览器
            webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
            webview.getSettings().setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
            webview.getSettings().setSupportZoom(true);//是否可以缩放，默认true
            webview.getSettings().setBuiltInZoomControls(true);//是否显示缩放按钮，默认false
            webview.getSettings().setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
            webview.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
            webview.getSettings().setAppCacheEnabled(true);//是否使用缓存
            webview.getSettings().setDomStorageEnabled(true);//DOM Storage
        //我就是电脑端我不管
        webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");

        final WebSettings webSettings=webview.getSettings();
            webSettings.setJavaScriptEnabled(true);//运行客户端运行服务器端js权限
            webview.setWebChromeClient(new WebChromeClient(){
                @Override
                public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                    AlertDialog.Builder builder=new AlertDialog.Builder(ZhiDaoDetailsActivity.this);
                    builder.setTitle("再错你要被我打！！");
                    builder.setMessage(message);
                    builder.setPositiveButton("好的", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            result.confirm();
                        }
                    });
                    builder.create().show();
                    return (true);
                }
            });
            webview.loadUrl(url);

        setToolBar("详情");
        mTencent = Tencent.createInstance(APP_ID, this.getApplicationContext());

        ImageView iv = findViewById(R.id.share_imageview);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ZhiDaoDetailsActivity.this, "点击了分享", Toast.LENGTH_SHORT).show();
                mainActivity = MainActivity.getMainActivity();
                UserUtil userUtil = new UserUtil(mainActivity);
                String Uid = userUtil.getUserId();
                if(Uid.equals("")){
                    Toast.makeText(mainActivity, "请先登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                OnClickShare onClickShare = new OnClickShare(ZhiDaoDetailsActivity.this,mTencent,url);
                onClickShare.share();
            }
        });
    }
    private void setToolBar(String title) {
        Toolbar toolbar =  findViewById(R.id.zhidaodetails_toolbar);
        toolbar.setTitle(title);
        this.setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        this.getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    //Toolbar的事件---返回
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

}
